﻿using BusyReports.ReportingService;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace BusyReportsGui
{
    /// <summary>
    /// Interaction logic for NewSubscription.xaml
    /// </summary>
    public partial class NewSubscription : Window
    {
        CatalogItem[] theCatalogItems;
        private ReportingService2010 theReportService;
        public string thePath;

        BackgroundWorker bw = new BackgroundWorker();

        public NewSubscription()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            lvReports.ItemsSource = new List<object> {new { Path=  "loading reports..." }};
            bw.DoWork += new DoWorkEventHandler(bw_DoWork);
            bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
            bw.RunWorkerAsync();

        }

        private void txtFilter_TextChanged(object sender, TextChangedEventArgs e)
        {

            if (theCatalogItems == null) return;

            var q = (from r in theCatalogItems
                     where r.Path.ToUpper().Contains(txtFilter.Text.ToUpper())
                     & r.TypeName == "Report"
                     select r).ToList();

            lvReports.ItemsSource = q;
        }

        private void txtFilter_GotFocus(object sender, RoutedEventArgs e)
        {
            TextBox tb = (TextBox)sender;
            tb.Text = string.Empty;
            tb.GotFocus -= txtFilter_GotFocus;
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                thePath = ((CatalogItem)lvReports.SelectedItem).Path;
            }
            catch (Exception)
            {
                return;
            } 
            this.Close();
        }

        private void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            theReportService = new ReportingService2010();

            theReportService.UseDefaultCredentials = true;


            try
            {
                theCatalogItems = theReportService.ListChildren("/", true);

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }


        }
        private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {

            if (theCatalogItems == null) return;
            var q = (from r in theCatalogItems
                     where r.TypeName == "Report"
                     select r).ToList();


            lvReports.ItemsSource = q;
        }

        private void lvReports_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            btnAdd_Click(sender, null);
        }


    }
}
